//
//  SINAStudentTabBarViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/20.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAStudentTabBarViewController.h"
#import "WISHomeTabBarView.h"
#import "WISHomeTabBarModel.h"
#import "SINAStudentMainViewController.h"
#import "SINAControllerUtils.h"
#import "JPUSHService.h"
#import "SINAStudentMyViewController.h"
#import "PTStudentLifeServiceViewController.h"

@interface SINAStudentTabBarViewController ()<WISHomeTabBarDelegate>
//自定义的TabBar
@property(nonatomic,strong) WISHomeTabBarView *homeTabBarView;
//TabBar item 选项
@property(nonatomic,strong) NSMutableArray *tabbarItemList;
@end

@implementation SINAStudentTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    if (UI_IS_IPHONE6PLUS) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_plus_bg"]  forBarMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_bg"]  forBarMetrics:UIBarMetricsDefault];
    }
    //加载控制器
    [self configViewController];
    //加载tabBar
    [self.tabBar addSubview:self.homeTabBarView];
    
    SINAUserModel *userModel =[[SINACommonUserHelper share]getUserModel];
    if (userModel!=nil) {
        DLog(@" 极光推送jpush 注册别名 %@",userModel.userUid);
        int y =100 +  (arc4random() % 101);
        NSString *uuid = [NSString stringWithFormat:@"%i",y];
        //        推送注册别名
        [JPUSHService setAlias:userModel.userUid completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            DLog(@" 极光推送 别名注册完成 seq %ld and iResCode %ld -- %@",(long)iResCode,seq ,uuid);
        } seq:101];
        
        //        推送注册本地通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jpushNotificationFunction:)name:@"jpushNotification" object:nil];
    }
}
- (void)jpushNotificationFunction:(NSNotification *)dic{
    
    
    UIViewController *currentController = [[SINAControllerUtils  alloc]currentViewController];
    
    DLog(@"极光推送通知------%@  当前显示 %@ ",dic.userInfo[@"pushMsgType"],currentController);
    
    NSString *pushMsgType = dic.userInfo[@"pushMsgType"];
    
    NSString *notificationWithName;
    
    if(pushMsgType!=nil&& ([pushMsgType isEqualToString:@"TECPUBLISHSCORE"]||[pushMsgType isEqualToString:@"TECLEAVEMSG"])){
        //TECPUBLISHSCORE 老师发布成绩
        if ([currentController isKindOfClass:[SINAStudentMainViewController class]]) {
            //主页面
            DLog(@"is sinastudent main view controller ");
            self.selectedIndex=2;
        }else if([currentController isKindOfClass:[PTStudentLifeServiceViewController class]] ){
            //生活服务
            self.selectedIndex=2;
        }else if([currentController isKindOfClass:[SINAStudentMyViewController class]] ){
            //个人页面
            
        }else{
            //其他页面
            [currentController.navigationController popToRootViewControllerAnimated:YES];
            [self.navigationController setNavigationBarHidden:NO animated:YES];
            self.selectedIndex=2;
        }
        notificationWithName=notificationKeyNameStuMy;
    }else if(pushMsgType!=nil&& ([pushMsgType isEqualToString:@"STUFORMTEAM"]||[pushMsgType isEqualToString:@"TECCOURSESTARTMSG"]|| [pushMsgType isEqualToString:@"TECALERTCOURSETIME"]||[pushMsgType isEqualToString:@"TECCOURSESIGN"])){
        //STUFORMTEAM 组队
        //TECCOURSESTARTMSG 开课通知
        //TECALERTCOURSETIME 调课
        //TECCOURSESIGN  点名签到
        if ([currentController isKindOfClass:[SINAStudentMainViewController class]]) {
            //主页面
            DLog(@"is sinastudent main view controller ");
        }else if([currentController isKindOfClass:[PTStudentLifeServiceViewController class]] ){
            //生活服务
            self.selectedIndex=0;
        }else if([currentController isKindOfClass:[SINAStudentMyViewController class]] ){
            //个人页面
            self.selectedIndex=0;
        }else{
            //其他页面
            [currentController.navigationController popToRootViewControllerAnimated:YES];
            [self.navigationController setNavigationBarHidden:NO animated:YES];
            self.selectedIndex=0;
        }
        notificationWithName=notificationKeyNameStuHome;
    }
    if (notificationWithName!=nil) {
        NSNotification *notification =[NSNotification notificationWithName:notificationWithName object:nil userInfo:dic.userInfo];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

//创建自定义的TabBar
-(WISHomeTabBarView *)homeTabBarView{
    if (!_homeTabBarView) {
        _homeTabBarView = [[WISHomeTabBarView alloc]initWithItem:self.tabbarItemList:CGRectMake(0, 0, mainW, 49):[UIColor grayColor]:baseSelectColor];
        _homeTabBarView.delegate=self;
    }
    return _homeTabBarView;
}
//自定义TabBar中Item的点击事件回调
-(void)tabbar:(WISHomeTabBarView *)tabBar clickButton:(WISHomeItemType)idx{
    //切换对应的UITabBarController中的Controller
    self.selectedIndex=idx-WISHomeItemTypeDefault;
}

-(NSMutableArray *)tabbarItemList{
    if (!_tabbarItemList) {
        _tabbarItemList = [NSMutableArray array];
        
        WISHomeTabBarModel *homeModel=[[WISHomeTabBarModel alloc]initWithName:@"肆迹" andImageNormalName:@"stu_home_item_nor" andImagePressName:@"stu_home_item_pre" imageSize: CGSizeMake(21, 24)];
        [_tabbarItemList addObject:homeModel];
        WISHomeTabBarModel *liveServiceModel=[[WISHomeTabBarModel alloc]initWithName:@"生活服务" andImageNormalName:@"stu_service_item_nor" andImagePressName:@"stu_service_item_pre" imageSize:CGSizeMake(31, 24)];
        [_tabbarItemList addObject:liveServiceModel];
        
        WISHomeTabBarModel *myModel=[[WISHomeTabBarModel alloc]initWithName:@"我的" andImageNormalName:@"stu_my_item_nor" andImagePressName:@"stu_my_item_pre" imageSize:CGSizeMake(19, 24)];
        [_tabbarItemList addObject:myModel];
    }
    return _tabbarItemList;
}

//加载控制器
-(void) configViewController{
    
    //保存的是已创建好的viewController的名称
    
    NSMutableArray *array=[NSMutableArray arrayWithArray:@[@"SINAStudentMainViewController",@"PTStudentLifeServiceViewController",@"SINAStudentMyViewController"]];
    for (NSInteger i=0; i<array.count; i++) {
        //取出新建好的ViewController的名称
        NSString *item = array[i];
        //构建ViewController
        UIViewController *itemController = [[NSClassFromString(item)alloc]init];
        //构建NavViewController
        CXBaseNavViewController *itemNavController =[[CXBaseNavViewController alloc]initWithRootViewController:itemController];
        //添加到集合中
        [array replaceObjectAtIndex:i withObject:itemNavController];
        
    }
    //将创建好的Controll关联UITabBarController
    self.viewControllers = array;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
